use std::{fs::File, io::Read, path::Path};

use crate::{parse, parse_exprs, runtime::EnvExt, Env, Result};

#[test]
fn run_id() -> crate::Result<()>{
    let global = Env::root();
    let val = global.eval(parse(read_file("examples/format_string.dj").unwrap())?)?;

    println!("{val}");
    Ok(())
}

fn read_file<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
    let mut src_file = File::open(path)?;
    let mut src = String::new();
    src_file.read_to_string(&mut src)?;
    Ok(src)
}

#[test]
fn expr_into_compound() -> Result<()> {
    let exprs = parse_exprs("(item 1) (block 1 2) (chunk 1 2 3)")?;
    for compound in exprs.into_iter().flat_map(|expr| expr.into_kv_pair()) {
        println!("({} {})", compound.0, compound.1)
    }
    Ok(())
}

#[test]
fn expr_eliminate() -> Result<()> {
    let mut expr = parse(r#"(+ "Hello" ", world")"#)?;
    expr.eliminate();
    println!("{}", expr);
    Ok(())
}

#[test]
fn readme_01() -> Result<()> {
    // 新建dj的运行环境。
    let global = Env::root();
    
    let ans = global.eval(parse("(+ 1 1)")?)?;

    println!("The answer is {ans}");

    Ok(())
}